<?
require("lib/Db.php");
$db=new Db();
$table='user';
$con="";
$start=isset($_GET['start'])?$_GET['start']:0;
$limit=isset($_GET['limit'])?$_GET['limit']:10;
if(isset($_GET['name']))
$con="name like %'".$_GET['name']."'% ";
$sql="";
if(isset($_GET['cmd']))
{
   //获取JSON
   if($_GET['cmd']=="List")
   {
    echo $db->toExtJson($table,$start,$limit,$con);
   }
   //更新
   if($_GET['cmd']=="Update")
   {
     $sql="update user set Name='".$_POST['Name']."',Password='".$_POST['Password']."',Email='".$_POST['Email']."',Remark='".$_POST['Remark']."' where Id='".$_POST['Id']."'";
     if($db->execute($sql))
	 echo"{success:true,info:'更新成功'}";
	 else
	 echo"{failure:true,info:'更新失败,错误原因为:".$db->getError()."'}";
   }
   //新增
   if($_GET['cmd']=="Save")
   {
     $sql="insert into user values(null,'".$_POST['Name']."','".$_POST['Password']."','".$_POST['Email']."','".$_POST['Remark']."')";
     if($db->execute($sql))
	 echo"{success:true,info:'添加成功'}";
	 else
	 echo"{failure:true,info:'添加失败,错误原因为:".$db->getError()."'}";
   }
   //批量删除
   if($_GET['cmd']=="Remove")
   {
    $rows=jsondecode($_GET['json']);
	foreach($rows as $row)
	{
	 $sql="delete from user where Id='".$row."'";
	 if(!$db->execute($sql))
	 {
	  echo"删除编号为：".$row."的用户失败";
	  break;
	 }
	}
	echo"删除成功";
   }

}
   //将JSON转为数组
   function jsondecode($jsonString)
    {
        require_once("lib/Ajax.php");
        $instance =& new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
        return $instance->decode($jsonString);
    }
?>